home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / src / termcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-21  |  15.9 KB  |  769 lines

  1. /* Work-alike for termcap, plus extra features.
  2.    Copyright (C) 1985, 1986, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Emacs config.h may rename various library functions such as malloc.  */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #include <paths.h>
  22. #else /* not HAVE_CONFIG_H */
  23.  
  24. #if defined(HAVE_STRING_H) || defined(STDC_HEADERS)
  25. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  26. #endif
  27.  
  28. #ifdef STDC_HEADERS
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #else
  32. char *getenv ();
  33. char *malloc ();
  34. char *realloc ();
  35. #endif
  36.  
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifdef _POSIX_VERSION
  41. #include <fcntl.h>
  42. #endif
  43.  
  44. #endif /* not HAVE_CONFIG_H */
  45.  
  46. #ifndef NULL
  47. #define NULL (char *) 0
  48. #endif
  49.  
  50. #ifndef O_RDONLY
  51. #define O_RDONLY 0
  52. #endif
  53.  
  54. /* BUFSIZE is the initial size allocated for the buffer
  55.    for reading the termcap file.
  56.    It is not a limit.
  57.    Make it large normally for speed.
  58.    Make it variable when debugging, so can exercise
  59.    increasing the space dynamically.  */
  60.  
  61. #ifndef BUFSIZE
  62. #ifdef DEBUG
  63. #define BUFSIZE bufsize
  64.  
  65. int bufsize = 128;
  66. #else
  67. #define BUFSIZE 2048
  68. #endif
  69. #endif
  70.  
  71. #ifndef TERMCAP_NAME
  72. #define TERMCAP_NAME "/etc/termcap"
  73. #endif
  74.  
  75. #ifndef emacs
  76. static void
  77. memory_out ()
  78. {
  79.   write (2, "virtual memory exhausted\n", 25);
  80.   exit (1);
  81. }
  82.  
  83. static char *
  84. xmalloc (size)
  85.      unsigned size;
  86. {
  87.   register char *tem = malloc (size);
  88.  
  89.   if (!tem)
  90.     memory_out ();
  91.   return tem;
  92. }
  93.  
  94. static char *
  95. xrealloc (ptr, size)
  96.      char *ptr;
  97.      unsigned size;
  98. {
  99.   register char *tem = realloc (ptr, size);
  100.  
  101.   if (!tem)
  102.     memory_out ();
  103.   return tem;
  104. }
  105. #endif /* not emacs */
  106.  
  107. /* Looking up capabilities in the entry already found.  */
  108.  
  109. /* The pointer to the data made by tgetent is left here
  110.    for tgetnum, tgetflag and tgetstr to find.  */
  111. static char *term_entry;
  112.  
  113. static char *tgetst1 ();
  114.  
  115. /* Search entry BP for capability CAP.
  116.    Return a pointer to the capability (in BP) if found,
  117.    0 if not found.  */
  118.  
  119. static char *
  120. find_capability (bp, cap)
  121.      register char *bp, *cap;
  122. {
  123.   for (; *bp; bp++)
  124.     if (bp[0] == ':'
  125.     && bp[1] == cap[0]
  126.     && bp[2] == cap[1])
  127.       return &bp[4];
  128.   return NULL;
  129. }
  130.  
  131. int
  132. tgetnum (cap)
  133.      char *cap;
  134. {
  135.   register char *ptr = find_capability (term_entry, cap);
  136.   if (!ptr || ptr[-1] != '#')
  137.     return -1;
  138.   return atoi (ptr);
  139. }
  140.  
  141. int
  142. tgetflag (cap)
  143.      char *cap;
  144. {
  145.   register char *ptr = find_capability (term_entry, cap);
  146.   return ptr && ptr[-1] == ':';
  147. }
  148.  
  149. /* Look up a string-valued capability CAP.
  150.    If AREA is non-null, it points to a pointer to a block in which
  151.    to store the string.  That pointer is advanced over the space used.
  152.    If AREA is null, space is allocated with `malloc'.  */
  153.  
  154. char *
  155. tgetstr (cap, area)
  156.      char *cap;
  157.      char **area;
  158. {
  159.   register char *ptr = find_capability (term_entry, cap);
  160.   if (!ptr || (ptr[-1] != '=' && ptr[-1] != '~'))
  161.     return NULL;
  162.   return tgetst1 (ptr, area);
  163. }
  164.  
  165. /* Table, indexed by a character in range 0100 to 0140 with 0100 subtracted,
  166.    gives meaning of character following \, or a space if no special meaning.
  167.    Eight characters per line within the string.  */
  168.  
  169. static char esctab[]
  170.   = " \007\010  \033\014 \
  171.       \012 \
  172.   \015 \011 \013 \
  173.         ";
  174.  
  175. /* PTR points to a string value inside a termcap entry.
  176.    Copy that value, processing \ and ^ abbreviations,
  177.    into the block that *AREA points to,
  178.    or to newly allocated storage if AREA is NULL.
  179.    Return the address to which we copied the value,
  180.    or NULL if PTR is NULL.  */
  181.  
  182. static char *
  183. tgetst1 (ptr, area)
  184.      char *ptr;
  185.      char **area;
  186. {
  187.   register char *p, *r;
  188.   register int c;
  189.   register int size;
  190.   char *ret;
  191.   register int c1;
  192.  
  193.   if (!ptr)
  194.     return NULL;
  195.  
  196.   /* `ret' gets address of where to store the string.  */
  197.   if (!area)
  198.     {
  199.       /* Compute size of block needed (may overestimate).  */
  200.       p = ptr;
  201.       while ((c = *p++) && c != ':' && c != '\n')
  202.     ;
  203.       ret = (char *) xmalloc (p - ptr + 1);
  204.     }
  205.   else
  206.     ret = *area;
  207.  
  208.   /* Copy the string value, stopping at null or colon.
  209.      Also process ^ and \ abbreviations.  */
  210.   p = ptr;
  211.   r = ret;
  212.   while ((c = *p++) && c != ':' && c != '\n')
  213.     {
  214.       if (c == '^')
  215.     c = *p++ & 037;
  216.       else if (c == '\\')
  217.     {
  218.       c = *p++;
  219.       if (c >= '0' && c <= '7')
  220.         {
  221.           c -= '0';
  222.           size = 0;
  223.  
  224.           while (++size < 3 && (c1 = *p) >= '0' && c1 <= '7')
  225.         {
  226.           c *= 8;
  227.           c += c1 - '0';
  228.           p++;
  229.         }
  230.         }
  231.       else if (c >= 0100 && c < 0200)
  232.         {
  233.           c1 = esctab[(c & ~040) - 0100];
  234.           if (c1 != ' ')
  235.         c = c1;
  236.         }
  237.     }
  238.       *r++ = c;
  239.     }
  240.   *r = '\0';
  241.   /* Update *AREA.  */
  242.   if (area)
  243.     *area = r + 1;
  244.   return ret;
  245. }
  246.  
  247. /* Outputting a string with padding.  */
  248.  
  249. short ospeed;
  250. /* If OSPEED is 0, we use this as the actual baud rate.  */
  251. int tputs_baud_rate;
  252. char PC;
  253.  
  254. /* Actual baud rate if positive;
  255.    - baud rate / 100 if negative.  */
  256.  
  257. static short speeds[] =
  258.   {
  259. #ifdef VMS
  260.     0, 50, 75, 110, 134, 150, -3, -6, -12, -18,
  261.     -20, -24, -36, -48, -72, -96, -192
  262. #else /* not VMS */
  263.     0, 50, 75, 110, 135, 150, -2, -3, -6, -12,
  264.     -18, -24, -48, -96, -192, -384
  265. #endif /* not VMS */
  266.   };
  267.  
  268. void
  269. tputs (str, nlines, outfun)
  270.      register char *str;
  271.      int nlines;
  272.      register int (*outfun) ();
  273. {
  274.   register int padcount = 0;
  275.   register int speed;
  276.  
  277. #ifdef emacs
  278.   extern baud_rate;
  279.   speed = baud_rate;
  280. #else
  281.   if (ospeed == 0)
  282.     speed = tputs_baud_rate;
  283.   else
  284.     speed = speeds[ospeed];
  285. #endif
  286.  
  287.   if (!str)
  288.     return;
  289.  
  290.   while (*str >= '0' && *str <= '9')
  291.     {
  292.       padcount += *str++ - '0';
  293.       padcount *= 10;
  294.     }
  295.   if (*str == '.')
  296.     {
  297.       str++;
  298.       padcount += *str++ - '0';
  299.     }
  300.   if (*str == '*')
  301.     {
  302.       str++;
  303.       padcount *= nlines;
  304.     }
  305.   while (*str)
  306.     (*outfun) (*str++);
  307.  
  308.   /* padcount is now in units of tenths of msec.  */
  309.   padcount *= speeds[ospeed];
  310.   padcount += 500;
  311.   padcount /= 1000;
  312.   if (speeds[ospeed] < 0)
  313.     padcount = -padcount;
  314.   else
  315.     {
  316.       padcount += 50;
  317.       padcount /= 100;
  318.     }
  319.  
  320.   while (padcount-- > 0)
  321.     (*outfun) (PC);
  322. }
  323.  
  324. /* Finding the termcap entry in the termcap data base.  */
  325.  
  326. struct buffer
  327.   {
  328.     char *beg;
  329.     int size;
  330.     char *ptr;
  331.     int ateof;
  332.     int full;
  333.   };
  334.  
  335. /* Forward declarations of static functions.  */
  336.  
  337. static int scan_file ();
  338. static char *gobble_line ();
  339. static int compare_contin ();
  340. static int name_match ();
  341.  
  342. #ifdef VMS
  343.  
  344. #include <rmsdef.h>
  345. #include <fab.h>
  346. #include <nam.h>
  347.  
  348. static int
  349. valid_filename_p (fn)
  350.      char *fn;
  351. {
  352.   struct FAB fab = cc$rms_fab;
  353.   struct NAM nam = cc$rms_nam;
  354.   char esa[NAM$C_MAXRSS];
  355.  
  356.   fab.fab$l_fna = fn;
  357.   fab.fab$b_fns = strlen(fn);
  358.   fab.fab$l_nam = &nam;
  359.   fab.fab$l_fop = FAB$M_NAM;
  360.  
  361.   nam.nam$l_esa = esa;
  362.   nam.nam$b_ess = sizeof esa;
  363.  
  364.   return SYS$PARSE(&fab, 0, 0) == RMS$_NORMAL;
  365. }
  366.  
  367. #else /* !VMS */
  368.  
  369. #ifdef MSDOS /* MW, May 1993 */
  370. static int
  371. valid_filename_p (fn)
  372.      char *fn;
  373. {
  374.   return *fn == '/' || fn[1] == ':';
  375. }
  376. #else
  377. #ifdef AMIGA
  378. static int
  379. valid_filename_p (fn)
  380.      char *fn;
  381. {
  382.   return (fn && (index(fn+1, ':')));
  383. }
  384. #else /* not AMIGA */
  385. #define valid_filename_p(fn) (*(fn) == '/')
  386. #endif
  387. #endif /* not AMIGA */
  388. #endif /* !VMS */
  389.  
  390. /* Find the termcap entry data for terminal type NAME
  391.    and store it in the block that BP points to.
  392.    Record its address for future use.
  393.  
  394.    If BP is null, space is dynamically allocated.
  395.  
  396.    Return -1 if there is some difficulty accessing the data base
  397.    of terminal types,
  398.    0 if the data base is accessible but the type NAME is not defined
  399.    in it, and some other value otherwise.  */
  400.  
  401. int
  402. tgetent (bp, name)
  403.      char *bp, *name;
  404. {
  405.   register char *termcap_name;
  406.   register int fd;
  407.   struct buffer buf;
  408.   register char *bp1;
  409.   char *bp2;
  410.   char *term;
  411.   int malloc_size = 0;
  412.   register int c;
  413.   char *tcenv;            /* TERMCAP value, if it contains :tc=.  */
  414.   char *indirect = NULL;    /* Terminal type in :tc= in TERMCAP value.  */
  415.   int filep;
  416.  
  417. #ifdef INTERNAL_TERMINAL
  418.   /* For the internal terminal we don't want to read any termcap file,
  419.      so fake it.  */
  420.   if (!strcmp (name, "internal"))
  421.     {
  422.       term = INTERNAL_TERMINAL;
  423.       if (!bp)
  424.     {
  425.       malloc_size = 1 + strlen (term);
  426.       bp = (char *) xmalloc (malloc_size);
  427.     }
  428.       strcpy (bp, term);
  429.       goto ret;
  430.     }
  431. #endif /* INTERNAL_TERMINAL */
  432.  
  433.   termcap_name = getenv ("TERMCAP");
  434.   if (termcap_name && *termcap_name == '\0')
  435.     termcap_name = NULL;
  436. #if defined (MSDOS) && !defined (TEST)
  437.   if (termcap_name && (*termcap_name == '\\'
  438.                || *termcap_name == '/'
  439.                || termcap_name[1] == ':'))
  440.     dostounix_filename(termcap_name);
  441. #endif
  442.  
  443. #ifdef AMIGA
  444.   filep = termcap_name != NULL; /* assume ok */
  445. #else
  446.   filep = termcap_name && valid_filename_p (termcap_name);
  447. #endif
  448.   
  449.   /* If termcap_name is non-null and starts with / (in the un*x case, that is),
  450.      it is a file name to use instead of /etc/termcap.
  451.      If it is non-null and does not start with /,
  452.      it is the entry itself, but only if
  453.      the name the caller requested matches the TERM variable.  */
  454.  
  455.   if (termcap_name && !filep && !strcmp (name, getenv ("TERM")))
  456.     {
  457.       indirect = tgetst1 (find_capability (termcap_name, "tc"), (char **) 0);
  458.       if (!indirect)
  459.     {
  460.       if (!bp)
  461.         bp = termcap_name;
  462.       else
  463.         strcpy (bp, termcap_name);
  464.       goto ret;
  465.     }
  466.       else
  467.     {            /* It has tc=.  Need to read /etc/termcap.  */
  468.       tcenv = termcap_name;
  469.        termcap_name = NULL;
  470.     }
  471.     }
  472.  
  473.   if (!termcap_name || !filep)
  474.     termcap_name = TERMCAP_NAME;
  475.  
  476.   /* Here we know we must search a file and termcap_name has its name.  */
  477.  
  478. #ifdef MSDOS
  479.   fd = open (termcap_name, O_RDONLY|O_TEXT, 0);
  480. #else
  481.   fd = open (termcap_name, O_RDONLY, 0);
  482. #endif
  483.   if (fd < 0)
  484.     return -1;
  485.  
  486.   buf.size = BUFSIZE;
  487.   /* Add 1 to size to ensure room for terminating null.  */
  488.   buf.beg = (char *) xmalloc (buf.size + 1);
  489.   term = indirect ? indirect : name;
  490.  
  491.   if (!bp)
  492.     {
  493.       malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
  494.       bp = (char *) xmalloc (malloc_size);
  495.     }
  496.   bp1 = bp;
  497.  
  498.   if (indirect)
  499.     /* Copy the data from the environment variable.  */
  500.     {
  501.       strcpy (bp, tcenv);
  502.       bp1 += strlen (tcenv);
  503.     }
  504.  
  505.   while (term)
  506.     {
  507.       /* Scan the file, reading it via buf, till find start of main entry.  */
  508.       if (scan_file (term, fd, &buf) == 0)
  509.     {
  510.       close (fd);
  511.       free (buf.beg);
  512.       if (malloc_size)
  513.         free (bp);
  514.       return 0;
  515.     }
  516.  
  517.       /* Free old `term' if appropriate.  */
  518.       if (term != name)
  519.     free (term);
  520.  
  521.       /* If BP is malloc'd by us, make sure it is big enough.  */
  522.       if (malloc_size)
  523.     {
  524.       malloc_size = bp1 - bp + buf.size;
  525.       termcap_name = (char *) xrealloc (bp, malloc_size);
  526.       bp1 += termcap_name - bp;
  527.       bp = termcap_name;
  528.     }
  529.  
  530.       bp2 = bp1;
  531.  
  532.       /* Copy the line of the entry from buf into bp.  */
  533.       termcap_name = buf.ptr;
  534.       while ((*bp1++ = c = *termcap_name++) && c != '\n')
  535.     /* Drop out any \ newline sequence.  */
  536.     if (c == '\\' && *termcap_name == '\n')
  537.       {
  538.         bp1--;
  539.         termcap_name++;
  540.       }
  541.       *bp1 = '\0';
  542.  
  543.       /* Does this entry refer to another terminal type's entry?
  544.      If something is found, copy it into heap and null-terminate it.  */
  545.       term = tgetst1 (find_capability (bp2, "tc"), (char **) 0);
  546.     }
  547.  
  548.   close (fd);
  549.   free (buf.beg);
  550.  
  551.   if (malloc_size)
  552.     bp = (char *) xrealloc (bp, bp1 - bp + 1);
  553.  
  554.  ret:
  555.   term_entry = bp;
  556.   if (malloc_size)
  557.     return (int) bp;
  558.   return 1;
  559. }
  560.  
  561. /* Given file open on FD and buffer BUFP,
  562.    scan the file from the beginning until a line is found
  563.    that starts the entry for terminal type STR.
  564.    Return 1 if successful, with that line in BUFP,
  565.    or 0 if no entry is found in the file.  */
  566.  
  567. static int
  568. scan_file (str, fd, bufp)
  569.      char *str;
  570.      int fd;
  571.      register struct buffer *bufp;
  572. {
  573.   register char *end;
  574.  
  575.   bufp->ptr = bufp->beg;
  576.   bufp->full = 0;
  577.   bufp->ateof = 0;
  578.   *bufp->ptr = '\0';
  579.  
  580.   lseek (fd, 0L, 0);
  581.  
  582.   while (!bufp->ateof)
  583.     {
  584.       /* Read a line into the buffer.  */
  585.       end = NULL;
  586.       do
  587.     {
  588.       /* if it is continued, append another line to it,
  589.          until a non-continued line ends.  */
  590.       end = gobble_line (fd, bufp, end);
  591.     }
  592.       while (!bufp->ateof && end[-2] == '\\');
  593.  
  594.       if (*bufp->ptr != '#'
  595.       && name_match (bufp->ptr, str))
  596.     return 1;
  597.  
  598.       /* Discard the line just processed.  */
  599.       bufp->ptr = end;
  600.     }
  601.   return 0;
  602. }
  603.  
  604. /* Return nonzero if NAME is one of the names specified
  605.    by termcap entry LINE.  */
  606.  
  607. static int
  608. name_match (line, name)
  609.      char *line, *name;
  610. {
  611.   register char *tem;
  612.  
  613.   if (!compare_contin (line, name))
  614.     return 1;
  615.   /* This line starts an entry.  Is it the right one?  */
  616.   for (tem = line; *tem && *tem != '\n' && *tem != ':'; tem++)
  617.     if (*tem == '|' && !compare_contin (tem + 1, name))
  618.       return 1;
  619.  
  620.   return 0;
  621. }
  622.  
  623. static int
  624. compare_contin (str1, str2)
  625.      register char *str1, *str2;
  626. {
  627.   register int c1, c2;
  628.   while (1)
  629.     {
  630.       c1 = *str1++;
  631.       c2 = *str2++;
  632.       while (c1 == '\\' && *str1 == '\n')
  633.     {
  634.       str1++;
  635.       while ((c1 = *str1++) == ' ' || c1 == '\t');
  636.     }
  637.       if (c2 == '\0')
  638.     {
  639.       /* End of type being looked up.  */
  640.       if (c1 == '|' || c1 == ':')
  641.         /* If end of name in data base, we win.  */
  642.         return 0;
  643.       else
  644.         return 1;
  645.         }
  646.       else if (c1 != c2)
  647.     return 1;
  648.     }
  649. }
  650.  
  651. /* Make sure that the buffer <- BUFP contains a full line
  652.    of the file open on FD, starting at the place BUFP->ptr
  653.    points to.  Can read more of the file, discard stuff before
  654.    BUFP->ptr, or make the buffer bigger.
  655.  
  656.    Return the pointer to after the newline ending the line,
  657.    or to the end of the file, if there is no newline to end it.
  658.  
  659.    Can also merge on continuation lines.  If APPEND_END is
  660.    non-null, it points past the newline of a line that is
  661.    continued; we add another line onto it and regard the whole
  662.    thing as one line.  The caller decides when a line is continued.  */
  663.  
  664. static char *
  665. gobble_line (fd, bufp, append_end)
  666.      int fd;
  667.      register struct buffer *bufp;
  668.      char *append_end;
  669. {
  670.   register char *end;
  671.   register int nread;
  672.   register char *buf = bufp->beg;
  673.   register char *tem;
  674.  
  675.   if (!append_end)
  676.     append_end = bufp->ptr;
  677.  
  678.   while (1)
  679.     {
  680.       end = append_end;
  681.       while (*end && *end != '\n') end++;
  682.       if (*end)
  683.         break;
  684.       if (bufp->ateof)
  685.     return buf + bufp->full;
  686.       if (bufp->ptr == buf)
  687.     {
  688.       if (bufp->full == bufp->size)
  689.         {
  690.           bufp->size *= 2;
  691.           /* Add 1 to size to ensure room for terminating null.  */
  692.           tem = (char *) xrealloc (buf, bufp->size + 1);
  693.           bufp->ptr = (bufp->ptr - buf) + tem;
  694.           append_end = (append_end - buf) + tem;
  695.           bufp->beg = buf = tem;
  696.         }
  697.     }
  698.       else
  699.     {
  700.       append_end -= bufp->ptr - buf;
  701.       bcopy (bufp->ptr, buf, bufp->full -= bufp->ptr - buf);
  702.       bufp->ptr = buf;
  703.     }
  704.       if (!(nread = read (fd, buf + bufp->full, bufp->size - bufp->full)))
  705.     bufp->ateof = 1;
  706.       bufp->full += nread;
  707.       buf[bufp->full] = '\0';
  708.     }
  709.   return end + 1;
  710. }
  711.  
  712. #ifdef TEST
  713.  
  714. #ifdef NULL
  715. #undef NULL
  716. #endif
  717.  
  718. #include <stdio.h>
  719.  
  720. main (argc, argv)
  721.      int argc;
  722.      char **argv;
  723. {
  724.   char *term;
  725.   char *buf;
  726.  
  727.   term = argv[1];
  728.   printf ("TERM: %s\n", term);
  729.  
  730.   buf = (char *) tgetent (0, term);
  731.   if ((int) buf <= 0)
  732.     {
  733.       printf ("No entry.\n");
  734.       return 0;
  735.     }
  736.  
  737.   printf ("Entry: %s\n", buf);
  738.  
  739.   tprint ("cm");
  740.   tprint ("AL");
  741.  
  742.   printf ("co: %d\n", tgetnum ("co"));
  743.   printf ("am: %d\n", tgetflag ("am"));
  744. }
  745.  
  746. tprint (cap)
  747.      char *cap;
  748. {
  749.   char *x = tgetstr (cap, 0);
  750.   register char *y;
  751.  
  752.   printf ("%s: ", cap);
  753.   if (x)
  754.     {
  755.       for (y = x; *y; y++)
  756.     if (*y <= ' ' || *y == 0177)
  757.       printf ("\\%0o", *y);
  758.     else
  759.       putchar (*y);
  760.       free (x);
  761.     }
  762.   else
  763.     printf ("none");
  764.   putchar ('\n');
  765. }
  766.  
  767. #endif /* TEST */
  768.  
  769.